// pages/user/find/findBask/findBask.js
const app = getApp();
//解析html
const WxParse = require('../../../../wxParse/wxParse.js');
import Api from '../../../../utils/http';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    sunList: [],
    searchFlag: true,
    input: '', //搜索关键
    page: 1, //页码
    pageSize: 9, //每页显示条数
  },
  //跳转 搜索关键字
  toSearchKeyWord: function () {
    //点击输入框
    this.setData({
      searchFlag: false
    })
  },
  //获取输入关键字
  input: function (e) {
    var that = this;
    that.setData({
      input: e.detail.value
    });
    this.getSearch();
  },
  //跳转至发布晒一晒
  release:function(){
    this.getusertext();
  },
  //跳转晒一晒详情
  tobaskDetail: function (e) {
    var that = this;
    var {
      sunList
    } = that.data;
    let index = e.currentTarget.dataset.index;
    let lid=sunList[index].id;
    // let 
    console.log(lid);
    wx.navigateTo({
      url: `/pages/user/find/baskDetail/baskDetail?id=${lid}`,
    })
  },
  //点赞
  fabulous: function (e) {
    var that = this;
    let is_click=e.currentTarget.dataset.click;
    console.log(e,is_click);
    if (is_click == 0) {
      console.log(1);
      this.getClickStatus(e);
    } else {
      console.log(2);
      this.getClick(e);
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.getSun();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
        // that.getSun();

  },
  //个人信息详情
  getusertext: function () {
    var that = this;
    let uid = wx.getStorageSync('userid');
    var param = {
      uid: uid.id,
      user_type: uid.user_type,
    };
    console.log(param)
    Api.ajax("/app/Zpersonalcenter/userInfo", param).then(res => {
      console.log(res)
      if (res.code == 200) {
        wx.setStorageSync('userid', res.data);
        if (res.data.verification==0) {
          wx.showModal({
            title: '提示',
            content: '该用户未认证，点击确定前往认证',
            success (res) {
              if (res.confirm) {
                wx.navigateTo({
                  url: `/pages/user/user/mobileAuth/mobileAuth`,
                })
              } else if (res.cancel) {
                wx.showToast({
                  title: '用户未认证',
                })
              }
            }
          })
        }else{
          wx.navigateTo({
            url: `/pages/user/find/releaseSun/releaseSun`,
          })
        }
      }
    })
  },
  
  //点赞 
  getClickStatus: function (e) {
    var that = this;
    let uid = wx.getStorageSync('userid');
    var id = e.currentTarget.dataset.id;
    var param = {
      aid: id,
      uid: uid.id,
      type: 1
    }
    console.log(param)
    Api.ajax("/app/Zarticle/giveLook", param).then(res => {
      console.log(res)
      if (res.code == 200) {
        wx.showToast({
          title: '点赞成功',
          icon: 'none',
          duration: 1000
        })
        setTimeout(function(){
          that.refreshData();
        },1000)
      }else{
        wx.showToast({
          title: '已点赞',
          icon: 'none',
          duration: 1000
        })
      }
    })
  },
  //取消点赞 
  getClick: function (e) {
    var that = this;
    let uid = wx.getStorageSync('userid')
    var id = e.currentTarget.dataset.id;
    var param = {
      aid: id,
      uid: uid.id,
    }
    console.log(param)
    Api.ajax("/app/Zarticle/deleteClick", param).then(res => {
      console.log(res)
      if (res.code == 200) {
        wx.showToast({
          title: '点赞成功',
          icon: 'none',
          duration: 1000,
        })
        setTimeout(function () {
          that.refreshData();
        }, 2000);
      } else {
        wx.showToast({
          title: '已点赞',
          icon: 'none',
          duration: 1000,
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.refreshData();
  },
  /**下拉刷新*/
  refreshData() {
    var that = this;
    let uid = wx.getStorageSync('userid')
    var sunList = that.data.sunList;
    var param = {
      uid:uid.id,
      type: 3,
      pageNum: 1, // 页码默认为0
      pageSize: 6 // 每页显示合数，默认为10
    }
    Api.ajax('/app/Zarticle/articleList', param).then(res => {
      console.log(res)

      that.setData({
        sunList: res.data,
        page: 2 // 一定要记得重置页码 ---- 没有数据的提示（上拉加载提示过后）
      })
      // 真机测试的时候，下拉刷新技术需要停止 下拉刷新的操作
      wx.stopPullDownRefresh();
    }).catch((err) => {
      console.log(err)
    })
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getSun();
  },
  //搜索
  getSearch: function (e) {
    var that = this;
    var page = that.data.page;
    let uid = wx.getStorageSync('userid')
    var param = {
      uid:uid.id,
      type: 3,
      pageNum: 1,
      pageSize: that.data.pageSize,
      keyWord: that.data.input
    }
    console.log(param)
    Api.ajax("/app/Zarticle/articleList", param).then(res => {
      console.log(res)
      if (res.code == 200) {
        that.setData({
          sunList: res.data,
        });
      }
    })
  },
  //、晒一晒
  getSun: function () {
    var that = this;
    var sunlist = that.data.sunList;
    var page = that.data.page;
    let uid = wx.getStorageSync('userid')
    var param = {
      uid:uid.id,
      type: 3,
      pageNum: page,
      pageSize: that.data.pageSize,
    }
    Api.ajax("/app/Zarticle/articleList", param).then(res => {
      console.log(res)
      if (res.code == 200) {
        
        page++;
        that.setData({
          sunList: sunlist.concat(res.data),
          page: page,
        });
      } else {
        if (page == 1) {
          wx.showToast({
            icon: 'none',
            title: '暂时没有数据哦!',
          })
        } else {
          wx.showToast({
            icon: 'none',
            title: '没有更多了!',
          })
        }

      }
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})